﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Condimar.Entities;

namespace Condimar.Repository
{
    public class ClienteDAO:MasterRepository,IClienteDAO
    {
        public List<Cliente> GetAllFromClientes()
        {
            var query = from c in Context.Clientes
                        select c;
            return query.ToList();
        }

        public List<Cliente> GetFromClienteByNombres(string nombres)
        {
            if (nombres != "")
            {
                var query = from c in Context.Clientes
                            where c.NombreRazonSocial.ToUpper().Contains(nombres.ToUpper())
                            select c;
                return query.ToList();
            }
            else
            {
                var query1 = from c in Context.Clientes
                             select c;
                return query1.ToList();
            }
        }

        public List<Cliente> GetFromClienteByRuc(string ruc)
        {
            if (ruc != "")
            {
                var query = from c in Context.Clientes
                            where c.RucDni.ToUpper().Contains(ruc.ToUpper())
                            select c;
                return query.ToList();
            }
            else
            {
                var query1 = from c in Context.Clientes
                             select c;
                return query1.ToList();
            }
        }

        public Cliente GetFromClienteById(int id)
        {
            return Context.Clientes.Find(id);
        }

        public void AddCliente(Cliente cliente)
        {
            Context.Clientes.Add(cliente);
            Context.SaveChanges();
        }

        public void UpdateCliente(Cliente cliente)
        {
            Context.Entry(cliente).State = System.Data.EntityState.Modified;
            Context.SaveChanges();
        }

        public void RemoveCliente(int id)
        {
            var existe = from c in Context.Clientes
                         where c.Id.Equals(id)
                         select c;

            Context.Clientes.Remove(existe.SingleOrDefault());
            Context.SaveChanges();
        }
    }
}
